//
//  H_D_TabCell.m
//  Tang
//
//  Created by lanou on 15/12/7.
//  Copyright © 2015年 352324497@qq.com. All rights reserved.
//

#import "H_D_SecCell.h"
#define KPicUrl @"http://bt.img.17gwx.com/%@"
#define KFontSize (20 * KWidth / 414 )
#define Kspace  (10* KWidth / 414)
#define KcellWid (KWidth - Kspace * 2)
#define  labHei  (20 * KWidth / 320)





@interface H_D_SecCell ()
@property (nonatomic, strong) UILabel *titleLabel;//标题
@property (nonatomic, strong) UILabel *descrtionLabel;//内容描述
@property (nonatomic, strong) UIView *viewIcon;//图片view
@property (nonatomic, strong) UILabel *priceLabel;// 价格label
@property (nonatomic, strong) UILabel *lineLabel;//间隔线

@property (nonatomic, strong) UIImageView *ttIcon;//投标;
@property (nonatomic, strong) UILabel *ttlabel;//序号;
@end
@implementation H_D_SecCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = NO;
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.alpha = 0.7;
        
        self.titleLabel.font = [UIFont systemFontOfSize:KFontSize];
        self.descrtionLabel = [[UILabel alloc] init];
        self.descrtionLabel.font = [UIFont systemFontOfSize:KFontSize * 0.8];
        self.descrtionLabel.numberOfLines = 0;
        self.descrtionLabel.alpha = 0.7;
        
        self.viewIcon  = [[UIView alloc] init];
        self.priceLabel = [[UILabel alloc] init];
        self.priceLabel.textColor = [UIColor redColor];
        self.priceLabel.alpha = 0.8;
        self.priceLabel.textAlignment = NSTextAlignmentRight;
        
        self.lineLabel = [[UILabel alloc] init];
        self.lineLabel.backgroundColor = [UIColor colorWithRed:240 / 255.0 green:240 / 255.0 blue:240 / 255.0 alpha:1];
        self.lineLabel.alpha = 0.4;
        
        self.bottomView = [[UIView alloc] init];
        
        
        self.ttIcon = [[UIImageView alloc] init];
        self.ttIcon.image = [UIImage imageNamed:@"islike"];
        self.ttlabel = [[UILabel alloc] init];
        self.ttlabel.textAlignment = NSTextAlignmentCenter;
        self.ttlabel.font = [UIFont boldSystemFontOfSize:15*KWidth / 414];
        self.ttlabel.textColor = [UIColor whiteColor];
        [self.ttIcon addSubview:_ttlabel];
        
        
        
        CGFloat btnWidth = (KWidth - 4 * KWidth / 414) / 3;
        CGFloat bottomHeight = 40 * KWidth / 414;
       
       
        CGFloat y = 7 * KWidth / 414;
        CGFloat bW = btnWidth / 2 + 10 * KWidth / 414;
        CGFloat bx = (btnWidth - bW) / 2;
        self.comment = [[DIYButton alloc]initWithFrame:CGRectMake(bx, y, bW, bottomHeight - 2 * y)];
        self.comment.iconImageView.image = [UIImage imageNamed:@"comment"];
        self.comment.textLabel.font = [UIFont systemFontOfSize:18 * KWidth / 414];
        [self.bottomView addSubview:self.comment];
        
        CGFloat verX = 10 * KWidth / 414;
        CGFloat verW = 2 * KWidth / 414;
        CGFloat verH = (bottomHeight - 2 * verX);
        UIView *ver1 = [[UIView alloc]initWithFrame:CGRectMake(btnWidth, verX, verW, verH)];
        ver1.backgroundColor = [UIColor lightGrayColor];
        [self.bottomView addSubview:ver1];
        

        self.like = [[DIYButton alloc]initWithFrame:CGRectMake(btnWidth + 2 * KWidth / 414 + bx, y, bW + 20, bottomHeight - 2 * y)];
        self.like.iconImageView.image = [UIImage imageNamed:@"like"];
        self.like.textLabel.text = @"喜欢";
        self.comment.textLabel.font = [UIFont systemFontOfSize:18 * KWidth / 414];
        self.like.selectIconImageView.image = [UIImage imageNamed:@"islike"];
        [self.bottomView addSubview:self.like];
        
        UIView *ver2 = [[UIView alloc]initWithFrame:CGRectMake(btnWidth * 2 + verW, verX, verW, verH)];
        ver2.backgroundColor = [UIColor lightGrayColor];
        [self.bottomView addSubview:ver2];
        
        self.shop = [[DIYButton alloc]initWithFrame:CGRectMake(KWidth - btnWidth + bx, y, bW, bottomHeight - 2 * y)];
        self.shop.iconImageView.image = [UIImage imageNamed:@"shop"];
        self.shop.textLabel.text = @"购物";
        self.shop.textLabel.font = [UIFont systemFontOfSize:18 * KWidth / 414];
        [self.bottomView addSubview:self.shop];
        [self.contentView addSubview:self.bottomView];

        

        
        
        
        
        
        [self.contentView addSubview:_titleLabel];
        [self.contentView addSubview:_descrtionLabel];
        [self.contentView addSubview:_viewIcon];
        [self.contentView addSubview:_priceLabel];
        [self.contentView addSubview:_lineLabel];
        [self.contentView addSubview:self.bottomView];
        [self.contentView addSubview:_ttIcon];
    }
    return self;
}

- (void)setModel:(HomeDetailModel *)model num:(NSInteger)num
{
    _model = model;
    
    self.ttlabel.text = [NSString stringWithFormat:@"%02ld",num];
    
    self.titleLabel.text = model.title;
    
    self.priceLabel.text = [NSString stringWithFormat:@"参考价格: ￥%@",model.price];
    
            self.comment.textLabel.text = @"评论";
    
    
    
    //self.like.textLabel.text = _model.likes;
    if ([[CoreDataMan shareIntance]findSkuWithId:[NSString stringWithFormat:@"%ld",model.Id]])
    {
        self.like.selected = YES;
    }
    else
    {
        self.like.selected = NO;
    }

    
    [self upFrameWithModel];
}

- (void)upFrameWithModel
{
    CGFloat x = Kspace;
    CGFloat y = 0;
    
    self.ttIcon.frame = CGRectMake(x, y, 30* KWidth / 414, 30* KWidth / 414);
    self.ttlabel.frame = CGRectMake(0, 0, 30* KWidth / 414, 30* KWidth / 414);
    
    self.titleLabel.frame = CGRectMake(x + 40* KWidth / 414, y, KcellWid, labHei * 1.2);
    
    CGFloat descrHei = [CallTool heightWithLabel:_descrtionLabel ForNSStringWithString:_model.desc width:KcellWid fontSize:KFontSize * 0.8 space:6];
    
    y = self.titleLabel.height+ Kspace;
    
    self.descrtionLabel.frame = CGRectMake(x, y, KcellWid, descrHei);

    
    CGFloat imaghei = 0;
    for (UIImageView *imageView in self.viewIcon.subviews)
    {
        [imageView removeFromSuperview];
    }
    for (int i = 0; i< _model.picArray.count; i++)
    {
        
        
       PictureModel *picModel  = _model.picArray[i];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, imaghei, KcellWid, KcellWid / picModel.w *picModel.h)];
        imaghei += KcellWid / picModel.w *picModel.h + Kspace / 5;
        [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:KPicUrl, picModel.p]] placeholderImage:[UIImage imageNamed:@"PC"]];
        [self.viewIcon addSubview:imageView];
    }
    y = y + self.descrtionLabel.height + Kspace;
    
    self.viewIcon.frame =CGRectMake(x, y, KcellWid, imaghei);
    y = y + self.viewIcon.height + Kspace;
    
    self.priceLabel.frame = CGRectMake(x, y, KcellWid, labHei);
    
    
    y = y + self.priceLabel.height +Kspace;
    
    CGFloat bottomHeight = 40 * KWidth / 414;
    self.bottomView.frame = CGRectMake(x, y, KWidth, bottomHeight);
    
    
    

    y = y + self.bottomView.height + Kspace - 5;
    self.lineLabel.frame = CGRectMake(0, y, KWidth, 8 * KWidth /414);
}

+ (CGFloat)heightForCellWithModel:(HomeDetailModel *)model
{
    CGFloat imagHeight = 0 ;
    for (int i = 0; i< model.picArray.count; i++)
    {
        PictureModel * picModel = model.picArray[i];
        imagHeight += KcellWid / picModel.w * picModel.h + Kspace / 5;
    }

    
    CGFloat height = labHei + [CallTool heightForNSStringWithString:model.desc width:KcellWid fontSize:KFontSize * 0.8 space:6] + imagHeight + labHei + 40 * KWidth / 414;
    return height + Kspace *6;
}

@end
